export const isBoundaryPoly = (shape, state) => {
    let shapeInfo = shape.toJSON();
    const positions = shapeInfo.positions; // 多边形的顶点数组
    const imageRectangle = state.rangeImageLayer.options.rectangle;

    // 转换图片边界的经纬度为度数
    const west = Cesium.Math.toDegrees(imageRectangle.west);
    const east = Cesium.Math.toDegrees(imageRectangle.east);
    const south = Cesium.Math.toDegrees(imageRectangle.south);
    const north = Cesium.Math.toDegrees(imageRectangle.north);

    // 计算多边形的经纬度范围
    let minLon = Infinity, maxLon = -Infinity;
    let minLat = Infinity, maxLat = -Infinity;
    positions.forEach(pos => {
        const lon = pos[0];
        const lat = pos[1];
        if (lon < minLon) minLon = lon;
        if (lon > maxLon) maxLon = lon;
        if (lat < minLat) minLat = lat;
        if (lat > maxLat) maxLat = lat;
    });

    // 计算多边形和图片的跨度
    const polyWidth = maxLon - minLon;
    const imageWidth = east - west;
    const polyHeight = maxLat - minLat;
    const imageHeight = north - south;

    // 检查是否超出可平移调整的范围
    if (polyWidth > imageWidth || polyHeight > imageHeight) {
        console.log('多边形超出范围过大，无法保持形状不变');
        return;
    }

    // 计算平移量
    let deltaLon = 0;
    if (minLon < west) deltaLon = west - minLon; // 东移
    else if (maxLon > east) deltaLon = east - maxLon; // 西移

    let deltaLat = 0;
    if (minLat < south) deltaLat = south - minLat; // 北移
    else if (maxLat > north) deltaLat = north - maxLat; // 南移

    // 应用平移调整
    let adjusted = false;
    if (deltaLon !== 0 || deltaLat !== 0) {
        positions.forEach(pos => {
            pos[0] += deltaLon;
            pos[1] += deltaLat;
        });
        adjusted = true;
    }

    // 处理结果
    if (adjusted) {
        shape.positions = positions; // 更新顶点
        console.log('多边形已整体平移至边界内，形状未改变');
    } else {
        state.marks.push(shape); // 未超出则添加到标记
    }
}

export const isBoundaryRect = (shape, state) => {
    let shapeInfo = shape.toJSON();
    const originalPositions = shapeInfo.positions;  // 原始位置
    const width = Math.abs(originalPositions[1][0] - originalPositions[0][0]);  // 矩形宽度
    const height = Math.abs(originalPositions[1][1] - originalPositions[0][1]);  // 矩形高度

    // 获取a的范围
    const aMinLongitude = Math.min(originalPositions[0][0], originalPositions[1][0]);
    const aMaxLongitude = Math.max(originalPositions[0][0], originalPositions[1][0]);
    const aMinLatitude = Math.min(originalPositions[0][1], originalPositions[1][1]);
    const aMaxLatitude = Math.max(originalPositions[0][1], originalPositions[1][1]);

    // 获取图片矩形的边界
    const imageRectangle = state.rangeImageLayer.options.rectangle;

    // 判断是否超出范围，并调整位置
    let adjusted = false;

    // 超出西边界
    if (aMinLongitude < Cesium.Math.toDegrees(imageRectangle.west)) {
        const delta = Cesium.Math.toDegrees(imageRectangle.west) - aMinLongitude;
        shapeInfo.positions[0][0] = Cesium.Math.toDegrees(imageRectangle.west);
        shapeInfo.positions[1][0] = shapeInfo.positions[0][0] + width;
        adjusted = true;
    }
    // 超出东边界
    if (aMaxLongitude > Cesium.Math.toDegrees(imageRectangle.east)) {
        const delta = aMaxLongitude - Cesium.Math.toDegrees(imageRectangle.east);
        shapeInfo.positions[1][0] = Cesium.Math.toDegrees(imageRectangle.east);
        shapeInfo.positions[0][0] = shapeInfo.positions[1][0] - width;
        adjusted = true;
    }
    // 超出南边界
    if (aMinLatitude < Cesium.Math.toDegrees(imageRectangle.south)) {
        const delta = Cesium.Math.toDegrees(imageRectangle.south) - aMinLatitude;
        shapeInfo.positions[0][1] = Cesium.Math.toDegrees(imageRectangle.south);
        shapeInfo.positions[1][1] = shapeInfo.positions[0][1] + height;
        adjusted = true;
    }
    // 超出北边界
    if (aMaxLatitude > Cesium.Math.toDegrees(imageRectangle.north)) {
        const delta = aMaxLatitude - Cesium.Math.toDegrees(imageRectangle.north);
        shapeInfo.positions[1][1] = Cesium.Math.toDegrees(imageRectangle.north);
        shapeInfo.positions[0][1] = shapeInfo.positions[1][1] - height;
        adjusted = true;
    }

    // 如果有超出范围的情况，调整位置
    if (adjusted) {
        shape.positions = shapeInfo.positions;
        console.log('图形已自动调整至范围边界');
    } else {
        state.marks.push(shape);
    }
}

export const isBoundaryCircle = (shape, state) => {
    const shapeInfo = shape.toJSON();
    const originalCenter = shapeInfo.position; // 圆心坐标（经纬度）
    const radius = shapeInfo.style.radius;     // 半径（米）

    // 将半径转换为经纬度（度）
    const radiusInDegreesLat = radius / 111320;  // 纬度转换系数
    const radiusInDegreesLon = radius / (111320 * Math.cos(Cesium.Math.toRadians(originalCenter[1]))); // 水平经度转换系数

    // 圆心的经纬度
    const centerLon = originalCenter[0]; // 经度
    const centerLat = originalCenter[1]; // 纬度

    // 计算圆的矩形边界
    const minLon = centerLon - radiusInDegreesLon;
    const maxLon = centerLon + radiusInDegreesLon;
    const minLat = centerLat - radiusInDegreesLat;
    const maxLat = centerLat + radiusInDegreesLat;

    // 获取图像矩形的边界
    const imageRectangle = state.rangeImageLayer.options.rectangle;

    // 判断是否超出范围，并调整位置
    let adjusted = false;

    // 超出西边界
    if (minLon < Cesium.Math.toDegrees(imageRectangle.west)) {
        const delta = Cesium.Math.toDegrees(imageRectangle.west) - minLon;
        shapeInfo.position[0] += delta;  // 调整圆心经度
        adjusted = true;
    }

    // 超出东边界
    if (maxLon > Cesium.Math.toDegrees(imageRectangle.east)) {
        const delta = maxLon - Cesium.Math.toDegrees(imageRectangle.east);
        shapeInfo.position[0] -= delta;  // 调整圆心经度
        adjusted = true;
    }

    // 超出南边界
    if (minLat < Cesium.Math.toDegrees(imageRectangle.south)) {
        const delta = Cesium.Math.toDegrees(imageRectangle.south) - minLat;
        shapeInfo.position[1] += delta;  // 调整圆心纬度
        adjusted = true;
    }

    // 超出北边界
    if (maxLat > Cesium.Math.toDegrees(imageRectangle.north)) {
        const delta = maxLat - Cesium.Math.toDegrees(imageRectangle.north);
        shapeInfo.position[1] -= delta;  // 调整圆心纬度
        adjusted = true;
    }

    // 如果有超出范围的情况，调整位置
    if (adjusted) {
        shape.position = shapeInfo.position;
        console.log('圆形已自动调整至范围边界');
    } else {
        state.marks.push(shape);
    }
}

// 判断图形是否超出边界
export const isBoundary = (shape, state) => {
    if (!state.rangeImageLayer) {
        state.marks.push(shape);
        return;
    }
    if (shape.type === 'rectangle') {
        isBoundaryRect(shape, state)
    } else if (shape.type === 'polygon') {
        isBoundaryPoly(shape, state);
    } else if (shape.type === 'circle') {
        isBoundaryCircle(shape, state);
    } else if (shape.type === 'polyline') {
        isBoundaryPoly(shape, state);
    }
}
